package cn.lf.android.lawfirm.outgoingregistration;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import cn.lf.android.lawfirm.OrdinaryBaseActivity;
import cn.lf.android.lawfirm.R;
import cn.lf.android.lawfirm.changeapproval.ChangeApprovalListActivity;
import cn.lf.android.lawfirm.outgoingregistration.controller.OutGoingListConroller;
import cn.lf.android.lawfirm.outgoingregistration.modle.OutGoing;
import cn.lf.android.lawfirm.outgoingregistration.view.OutGoingRegistrationAdapter;
import cn.lf.android.lawfirm.utils.DebugLog;

import static android.R.id.list;
import static cn.lf.android.lawfirm.R.id.refreshLayout;

/**
 * Created by Administrator on 2017/8/22.
 * 外出登记列表
 */

public class OutGoingRegistrationActivity extends OrdinaryBaseActivity implements OutGoingListConroller.OutGoingListCallback {
    private ListView mListView;
    private OutGoingRegistrationAdapter mAdapter;
    private ArrayList<OutGoing> mList = new ArrayList<OutGoing>();
    private SmartRefreshLayout refreshLayout;
    private OutGoingListConroller mOutGoingListConroller;
    private int pageIndex = 1;
    public static Boolean isRefresh = false;

    @Override
    public int getChildView() {
        return R.layout.activity_outgoingregistration;
    }

    @Override
    public void initViewId() {
        refreshLayout = (SmartRefreshLayout) findId(R.id.refreshLayout);
        mListView = ((ListView) findId(R.id.lv_list));
        mListView.setOnItemClickListener(onItemClickListener);
        mAdapter = new OutGoingRegistrationAdapter(mList, mActivity);
        mListView.setAdapter(mAdapter);
        setLisenters();
    }

    @Override
    public void refreshLoad() {

    }

    AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            DebugLog.e("------点击了第-----" + i + "条数据");
            Intent intent = new Intent();
            intent.setClass(mActivity, OutGoingInfoActivity.class);
            startActivity(intent);

        }
    };

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.img_left:
                finish();
                break;
            case R.id.eb_tv_right://添加
                Intent intent = new Intent();
                intent.setClass(mActivity, AddOutGoingActivity.class);
                intent.putExtra("type", "0");
                startActivity(intent);
                break;
        }

    }

    @Override
    public void initData() {
        initTitle();
        mOutGoingListConroller = new OutGoingListConroller(this, this);
        mOutGoingListConroller.onOutGoingList("", String.valueOf(pageIndex));
    }

    private void setLisenters() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(0);//0秒
                pageIndex = 1;
                mList.clear();
                mOutGoingListConroller.onOutGoingList("", String.valueOf(pageIndex));
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(2000);
                pageIndex++;
                mOutGoingListConroller.onOutGoingList("", String.valueOf(pageIndex));
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(OutGoingRegistrationActivity.this, OutGoingInfoActivity.class);
                intent.putExtra("id", mList.get(position).id);
                startActivity(intent);
            }
        });
    }

    private void initTitle() {
        mSearchLayout.setVisibility(View.GONE);
        mImgLeft.setVisibility(View.VISIBLE);
        mImgLeft.setVisibility(View.VISIBLE);
        mTitle.setVisibility(View.VISIBLE);
        mTitle.setText("外出查询");
        mTvRight.setVisibility(View.VISIBLE);
        mTvRight.setText("添加");
        mImgLeft.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
    }

    /**
     * 跳转到编辑
     */
    public void onEdit(int pisition) {
        DebugLog.e("-------点击了编辑-------");
        Intent intent = new Intent();
        intent.setClass(mActivity, AddOutGoingActivity.class);
        intent.putExtra("type", "外出登记编辑");
        intent.putExtra("id", mList.get(pisition).id);
        startActivity(intent);
    }

    @Override
    public void onOutGoingListuccessed(List<OutGoing> beans) {
        mList.addAll(beans);
        mAdapter.notifyDataSetChanged();
        if (mList.size() == 0) {
            noData2.setVisibility(View.VISIBLE);
        } else {
            noData2.setVisibility(View.GONE);
        }
    }

    @Override
    public void onOutGoingListFailed() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (isRefresh) {
            if (null != refreshLayout) {
                refreshLayout.autoRefresh();
                isRefresh = false;
            }
        }
    }
}
